package com.netsdk.demo.customize;

import com.netsdk.demo.util.CaseMenu;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.PlaySDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.structure.DH_OUT_PTZ_VIEW_RANGE_STATUS;
import com.netsdk.lib.structure.NET_IN_VIEW_RANGE_STATE;
import com.netsdk.lib.structure.NET_OUT_VIEW_RANGE_STATE;
import com.netsdk.lib.utils.Initialization;
import com.sun.jna.Pointer;

public class ViewRangeStateDemo extends Initialization {


    /**变量区**/
    int channel=0;
    NetSDKLib.LLong attachHandle = new NetSDKLib.LLong(0);
    static NetSDKLib netsdkApi = NetSDKLib.NETSDK_INSTANCE;
    static NetSDKLib configApi = NetSDKLib.CONFIG_INSTANCE;
    static PlaySDKLib playsdkApi   = PlaySDKLib.PLAYSDK_INSTANCE;

    /**方法区**/
    public static class ViewRangeStateCallBack implements NetSDKLib.fViewRangeStateCallBack{

        private static ViewRangeStateCallBack instance;
        private ViewRangeStateCallBack() {}
        public static ViewRangeStateCallBack getInstance() {
            if (instance == null) {
                synchronized (ViewRangeStateCallBack.class) {
                    if (instance == null) {
                        instance = new ViewRangeStateCallBack();
                    }
                }
            }
            return instance;
        }
        @Override
        public void invoke(NetSDKLib.LLong lLoginID, NetSDKLib.LLong lAttachHandle, Pointer pBuf, int nBufLen, Pointer dwUser) {
            System.out.println("ViewRangeStateCallBack detected");
            System.out.println("lLoginID = " + lLoginID);
            System.out.println("lAttachHandle = " + lAttachHandle);
            DH_OUT_PTZ_VIEW_RANGE_STATUS msg = new DH_OUT_PTZ_VIEW_RANGE_STATUS();
            ToolKits.GetPointerData(pBuf, msg);
            System.out.println("msg = " + msg.toString());
        }
    }

    /**
     * 订阅云台可视域
     */
    public void AttachViewRangeState() {
        // MethodContent
        NET_IN_VIEW_RANGE_STATE stIn = new NET_IN_VIEW_RANGE_STATE();
        stIn.nChannel = channel;
        stIn.cbViewRange = ViewRangeStateCallBack.getInstance();
        stIn.write();
        NET_OUT_VIEW_RANGE_STATE stOut = new NET_OUT_VIEW_RANGE_STATE();
        stOut.write();
        attachHandle = netsdkApi.CLIENT_AttachViewRangeState(loginHandle, stIn.getPointer(), stOut.getPointer(), 3000);
        if(attachHandle.longValue() != 0){
            System.out.println("AttachViewRangeState succeed, AttachHandle = " + attachHandle.longValue());
        } else {
            System.out.println("AttachViewRangeState failed, error code is " + netsdkApi.CLIENT_GetLastError());
        }
    }

    /**
     * 取消订阅云台可视域
     */
    public void DetachViewRangeState() {

        if(netsdkApi.CLIENT_DetachViewRangeState(attachHandle)){
            System.out.println("DetachViewRangeState succeed");
        } else {
            System.out.println("DetachViewRangeState failed, error code is " + netsdkApi.CLIENT_GetLastError());
        }
    }

    public void RunTest()
    {
        System.out.println("Run Test");
        CaseMenu menu = new CaseMenu();;
        menu.addItem((new CaseMenu.Item(this , "AttachViewRangeState" , "AttachViewRangeState")));
        menu.addItem((new CaseMenu.Item(this , "DetachViewRangeState" , "DetachViewRangeState")));
        menu.run();
    }

    public static void main(String[] args) {
        ViewRangeStateDemo demo = new ViewRangeStateDemo();
        /**登录信息运行时输入**/
//        Scanner sc = new Scanner(System.in);
//        System.out.print("ip:");
//        String ip = sc.nextLine();
//        System.out.print("port:");
//        String tmp = sc.nextLine();
//        int port = Integer.parseInt(tmp);
//        System.out.print("username:");
//        String username = sc.nextLine();
//        System.out.print("password:");
//        String pwd = sc.nextLine();
//        InitTest(ip,port,username,pwd);
        /**登录信息固定**/
        InitTest("172.3.100.76",37777,"admin","admin123");
        demo.RunTest();
        LoginOut();

    }
}
